package com.noble.meether.ui.apply;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.noble.meether.R;
import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.meether.MeetUIShow;

/**
 * 提现页面
 */
public class ApplyAct extends BaseAppCompatActivity implements RequestComplete, View.OnClickListener, PObserver {
    private TextView tv_money, tv_account;
    private EditText et_num;

    private int channel;      // 提现来源
    private double amount;  // 用户金额
    private boolean hasAlipay = false; // 是否已经添加账户
    private UserDetail userDetail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_money_apply);
        initView();
    }

    private void initView() {
        setBackView("提现");
        MsgMgr.getInstance().attach(this);
        channel = getIntent().getIntExtra("channel", -1);

        et_num = findViewById(R.id.et_num);
        tv_money = findViewById(R.id.tv_money);
        tv_account = findViewById(R.id.tv_account);
        findViewById(R.id.btn_exchange).setOnClickListener(this);
        findViewById(R.id.apply_declare).setOnClickListener(this);
        tv_account.setOnClickListener(this);
        refreshView();
    }

    private void refreshView() {
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        amount = channel == -1 ? userDetail.getAmountCash() : userDetail.getAmountInvite();
        tv_money.setText(String.valueOf(amount));
        if (!TextUtils.isEmpty(userDetail.getAlipay())) {
            hasAlipay = true;
            tv_account.setText(userDetail.getAlipay());
        }
    }

    private void exchange() {
        if (!hasAlipay) {
            PToast.showShort("请先添加提现账户");
            return;
        }
        String strNum = et_num.getText().toString();
        if (TextUtils.isEmpty(strNum)) {
            PToast.showShort("请输入提现金额");
            return;
        }
        int num = Integer.valueOf(strNum);
        if (num < 50) {
            PToast.showShort("提现金额不少于50元");
            return;
        }
        if (num > amount) {
            PToast.showShort("可提现金额不足");
            return;
        }
        ModuleMgr.getCommonMgr().reqWithdraw(channel, strNum, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        PToast.showShort(response.getMsg());
        if (response.isOk()) {
            ModuleMgr.getCenterMgr().reqMyInfo("收益提现");
            finish();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_account:   // 添加或修改账户
                MeetUIShow.showApplyAccountAct(this);
                break;

            case R.id.btn_exchange:
                exchange();
                break;

            case R.id.apply_declare:  //跳转到提现说明页
                UIShow.showWithDrawExplainAct(this);
                break;
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                refreshView();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}
